package devicehandler

import (
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/devploys"
	"yundian/internel/app/models/devrule"
	"yundian/internel/app/models/devtype"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// ------- 设备类型相关API请求处理 deviceTypeHandler -------



// GetDeviceTypeList 获取设备类型列表API
func HandleGetDeviceTypeList(ctx *gin.Context)  {
	// 请求参数处理
	param := &request.DeviceTypeListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	// 获取数据
	data := dao.NewDeviceTypeDao().FetchDeviceTypeList(param)
	lists := data.Unwrap().([]*devtype.DeviceTypeModel)
	for _,lst := range lists {
		lst.RuleInfo = dao.NewDeviceRuleDao().GetRuleList(lst.Id).Unwrap().([]*devrule.DeviceRulesModel) // 计费规则
		lst.DrawInfo = dao.NewDeviceRuleDao().GetDrawList(lst.Id).Unwrap().(devploys.DevicePloysModel)          // 奖励规则
	}
	// 响应请求
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       lists,
	})(OK)
}

// HandleInsertDeviceType 新增设备类型API
func HandleInsertDeviceType(ctx *gin.Context)  {
	m := devtype.New()
	dingo.ErrorIf(ctx.ShouldBindJSON(m))
	m.CreateAt = dingo.Time(time.Now().Unix()) // 设备类型创建时间
	Response(ctx)("", CodeSuccess, dao.NewDeviceTypeDao().InsertDevType(m).Unwrap())(OK)
}

// HanleUpdateDeviceType 修改设备类型 API
func HanleUpdateDeviceType(ctx *gin.Context)  {
	m := devtype.New()
	dingo.ErrorIf(ctx.ShouldBindJSON(m))

	Response(ctx)("", CodeSuccess, dao.NewDeviceTypeDao().UpdateDevType(m).Unwrap())(OK)
}

// HandleDeleteDeviceType 删除设备类型API
func HandleDeleteDeviceType(ctx *gin.Context)  {
	param := &struct {
		Id int `json:"id" binding:"required,gt=0"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param))
	Response(ctx)("", CodeSuccess, dao.NewDeviceTypeDao().DeleteDeviceType(param.Id).Unwrap())(OK)
}

// HandleDeviceTypeStatus 更改设备类型状态
func HandleDeviceTypeStatus(ctx *gin.Context)  {
	param := &struct {
		Id     int  `json:"id" binding:"required,gt=0"`
		Status int8 `json:"status" binding:"required,gt=0"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("", CodeSuccess, dao.NewDeviceTypeDao().UpdateDeviceTypeStatus(param.Id, param.Status).Unwrap())(OK)
}

// HandleDeviceTypePriceConfig 添加设备类型的费用模板配置
func HandleDeviceTypePriceConfig(ctx *gin.Context)  {

}

// HandleDeviceTypeDrawConfig 添加设备类型的奖励模板配置
func HandleDeviceTypeDrawConfig(ctx *gin.Context)  {

}
